# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.utils.translation import ugettext as _
from django.core.validators import validate_comma_separated_integer_list
from member.models import Member
from wechat import consts

# Create your models here.


class Menu(models.Model):

    """微信菜单"""

    TYPE_CHOICES = (
        (1, 'click'),
        (2, 'view'),
        (3, 'scancode_push'),
        (4, 'scancode_waitmsg'),
        (5, 'pic_sysphoto'),
        (6, 'pic_photo_or_album'),
        (7, 'pic_weixin'),
        (8, 'location_select'),
        (9, 'media_id'),
        (10, 'view_limited')
    )

    LEVEL_CHOICES = (
        (1, 'button'),
        (2, 'sub_button')
    )
    member = models.ForeignKey(
        Member, on_delete=models.CASCADE, null=False, verbose_name='会员')
    mtype = models.IntegerField(
        default=2, choices=TYPE_CHOICES, verbose_name='菜单类型')
    mindex = models.PositiveSmallIntegerField(
        verbose_name='排序字段', default=0)
    name = models.CharField(max_length=120, verbose_name='菜单名称')
    key = models.CharField(max_length=128, null=True,
                           blank=True, verbose_name='推送消息')
    url = models.URLField(max_length=1024, null=True,
                          blank=True, verbose_name='url链接')
    media_id = models.CharField(
        max_length=100, null=True, blank=True, verbose_name='媒体id')
    appid = models.CharField(max_length=100, null=True,
                             blank=True, verbose_name='微信小程序id')
    pagepath = models.CharField(
        max_length=255, null=True, blank=True, verbose_name='微信小程序页面链接')
    level = models.PositiveIntegerField(
        default=1, choices=LEVEL_CHOICES, verbose_name='菜单等级')
    parent_id = models.IntegerField(default=0, verbose_name='上级菜单id')
    create = models.DateTimeField(
        auto_now_add=True, verbose_name='创建时间')
    modify = models.DateTimeField(
        auto_now=True, verbose_name='修改时间')

    class Meta:
        db_table = 'menu'
        verbose_name = '微信菜单'
        verbose_name_plural = '微信菜单'


class Wechat(models.Model):
    """
    公众号
    """
    WECHAT_TYPE_CHOICES = (
        (consts.WECHAT_TYPE_SUB, '订阅号'),
        (consts.WECHAT_TYPE_SUB2, '升级后的订阅号'),
        (consts.WECHAT_TYPE_SERVICE, '服务号'),
    )

    VERIFY_TYPE_CHOICES = (
        (consts.VERIFY_TYPE_NONE, '未认证'),
        (consts.VERIFY_TYPE_WECHAT, '微信认证'),
        (consts.VERIFY_TYPE_WEIBO, '新浪微博认证'),
        (consts.VERIFY_TYPE_TENCENT_WEIBO, '腾讯微博认证'),
        (consts.VERIFY_TYPE_BASE_VERIFY, '已资质认证通过但还未通过名称认证'),
        (consts.VERIFY_TYPE_3_1, '已资质认证通过、还未通过名称认证，但通过了新浪微博认证'),
        (consts.VERIFY_TYPE_3_2, '已资质认证通过、还未通过名称认证，但通过了腾讯微博认证'),
    )

    appid = models.CharField('公众号 ID', max_length=20, default='')
    alias = models.CharField('公众号名称', max_length=20, null=True, blank=True)
    service_type = models.IntegerField(
        '公众号类型', choices=WECHAT_TYPE_CHOICES,
        default=consts.WECHAT_TYPE_SUB
    )
    nick_name = models.CharField('昵称', max_length=32, null=True, blank=True)
    head_img = models.URLField('头像', max_length=256, null=True, blank=True)
    user_name = models.CharField('内部名称', max_length=32)
    qrcode_url = models.URLField(
        '二维码URL', max_length=256, null=True, blank=True)
    authorized = models.BooleanField('授权')
    verify_type = models.PositiveIntegerField(
        '认证类型', choices=VERIFY_TYPE_CHOICES)
    funcscope_categories = models.CharField(
        validators=[validate_comma_separated_integer_list], max_length=100, verbose_name='权限集')
    join_time = models.DateTimeField('授权时间', auto_now_add=True)
    create = models.DateTimeField(
        auto_now_add=True, verbose_name=_('create datetime'))
    modify = models.DateTimeField(
        auto_now=True, verbose_name=_('modify datetime'))

    class Meta:
        db_table = 'wechat'
        get_latest_by = 'join_time'
        verbose_name = '公众号'
        verbose_name_plural = '公众号'

    def __unicode__(self):
        return '公众号 {0}'.format(self.alias)

    def __str__(self):
        return self.__unicode__().encode('utf-8')

    def is_valid(self):
        return self.authorized


class Settings(models.Model):

    """公众号的个人配置"""

    member = models.ForeignKey(
        Member, on_delete=models.SET_NULL, null=True, verbose_name=_('member'))
    app_id = models.CharField(
        max_length=200, verbose_name=_('wechat application id'))
    app_secret = models.CharField(
        max_length=200, verbose_name=_('wechat application secret'))
    app_token = models.CharField(
        max_length=200, null=True, verbose_name=_('wechat application token'))
    encoding_aes_key = models.CharField(
        max_length=200, verbose_name=_('wechat encoding aes key'))
    create = models.DateTimeField(
        auto_now_add=True, verbose_name=_('create datetime'))
    modify = models.DateTimeField(
        auto_now=True, verbose_name=_('modify datetime'))

    class Meta:
        db_table = 'wechat_settins'
        verbose_name = '公众号个人设置'
        verbose_name_plural = '公众号个人设置'
